package DataBase;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Vector;

import DataObjects.BusinessMenuRightList;

public class UpdateCurrentMenu {
	private Vector<BusinessMenuRightList> menuDetails;
	private Vector<String> menuDetails1;
	private int i=0;
	private int status=0;
	
	public UpdateCurrentMenu(Vector<BusinessMenuRightList> menuDetail,Vector<String> menuDetail1)
	{
		
		this.menuDetails=menuDetail;
		this.menuDetails1=menuDetail1;
		
		
		
	}
	




public void saveNewBusinessMenu()
{
	SQLConnection sc=new SQLConnection();
	try {
		
				
		PreparedStatement ps2 = sc.getConnection().prepareStatement("UPDATE  menu bm SET bm.Mname=?,bm.Mprice=?,bm.Mstatus=?  where bm.Mid =?;");	
		sc.getConnection().setAutoCommit(false);
		ps2.setString(1,(String) menuDetails1.get(1));
		ps2.setString(2,(String) menuDetails1.get(2));
		ps2.setString(3,(String) menuDetails1.get(3));
		ps2.setString(4,(String) menuDetails1.get(0));
		ps2.executeUpdate();
		sc.getConnection().commit();
		ps2.close();
		PreparedStatement ps1 = sc.getConnection().prepareStatement("Delete FROM recipe_in_menu Where  Mid =? ;");	
		sc.getConnection().setAutoCommit(false);
		ps1.setString(1,(String) menuDetails1.get(0));
		ps1.executeUpdate();
		//sc.getConnection().commit();
		
		while(i<menuDetails.size())
		{
			status=0;		
			PreparedStatement ps = sc.getConnection().prepareStatement("Insert Into recipe_in_menu (Mid,Rid,role,isSelectable,rquantity) values(?,?,?,?,?);");
			
			ps.setString(1,(String) menuDetails1.get(0));
			ps.setString(2,Integer.toString( menuDetails.get(i).getRid()));
			ps.setString(3, menuDetails.get(i).getRole());
			
			if(menuDetails.get(i).getIsSelectable().equals("לבחירה")){
		    	status=1;}
			if(menuDetails.get(i).getIsSelectable().equals("קבוע")){
				status=0;	}
			ps.setString(4,Integer.toString(status));
			//ps.setString(5,(String) menuDetails.get(i).get(3));
			ps.setString(5,Integer.toString( menuDetails.get(i).getQuantity()));
			ps.executeUpdate();
			
			status=1;	
			ps1.close();
			ps.close();
			i++;
				
		}
		
		
		sc.getConnection().commit();
		sc.getConnection().close();
	}
	
	catch (SQLException e) {
		// TODO Auto-generated catch block
		status=0;
		e.printStackTrace();
	}
	
	
	}





public int getStatus() {
	return status;
}





public void setStatus(int status) {
	this.status = status;
}
	

	

}
